package com.uchengwang.ui.utils;

import android.Manifest;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;

import com.tbruyelle.rxpermissions.Permission;
import com.tbruyelle.rxpermissions.RxPermissions;
import com.uchengwang.ui.listener.PermissionsListener;

import rx.functions.Action1;

/**
 * Created by U城网 on 2017/7/13.
 */

public class PermissionUtils {
    private final String PACKAGE_URL_SCHEME = "package:";
    public static PermissionUtils utils;

    private PermissionUtils (){}

    public static PermissionUtils getUtils() {
        if (utils == null){
            utils = new PermissionUtils();
        }
        return utils;
    }

    /**
     * 判断或者请求获取录音权限
     * */
    public void checkRecord(Activity activity, PermissionsListener listener){
        requestPermissions(activity, "该功能缺少录音权限, ", listener, Manifest.permission.RECORD_AUDIO);
    }

    /**
     * 判断或者请求获取定位权限
     * */
    public void checkLocation(Activity activity, PermissionsListener listener){
        requestPermissions(activity, "该功能缺少定位权限, ", listener, Manifest.permission.ACCESS_COARSE_LOCATION);
    }

    /**
     * 检查获取文件[照片等的权限]
     * */
    public void checkStorage(Activity activity, PermissionsListener listener){
        requestPermissions(activity, "该功能缺少获取文件权限, ", listener, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    }

    /**
     * 检查读取文件[照片等的权限]
     * */
    public void checkRead(Activity activity, PermissionsListener listener){
        requestPermissions(activity, "该功能缺少获取文件权限, ", listener, Manifest.permission.READ_EXTERNAL_STORAGE);
    }




    //手机高版本获取权限
    public void requestPermissions(final Activity activity, final String msg, final PermissionsListener listener, String... permissions) {
        if (Build.VERSION.SDK_INT >= 23) {
            new RxPermissions(activity).requestEach(permissions)
                    .subscribe(new Action1<Permission>() {
                        @Override
                        public void call(Permission permission) {
                            if (permission.granted) {
                                listener.onSuccess();
                            } else {
                                new AlertDialog.Builder(activity)
                                        .setTitle("帮助")
                                        .setMessage(msg+"\n请点击\"设置\"-\"权限\"-打开所需权限。")
                                        .setPositiveButton("设置", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                dialog.cancel();
                                                Intent intent = new Intent(Settings
                                                        .ACTION_APPLICATION_DETAILS_SETTINGS);
                                                intent.setData(Uri.parse(PACKAGE_URL_SCHEME + activity.getPackageName()));
                                                activity.startActivity(intent);
                                            }
                                        })
                                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                dialog.cancel();
                                                listener.onFailed();
                                            }
                                        }).show();
                            }
                        }
                    });
        } else {
            listener.onSuccess();
        }
    }

}
